/**
 * Created by Celebration-Grey on 2017/5/7.
 */

var user = {
    _this : this,
    limit : 20,
    parame : {
        orderBy : 'id',
        order : 'asc',
        index : 1,
        limit : this.limit
    },
    urls : {
        getUsers : host + 'admin_getAllUsers'
    },
    $table : this,
    $thead : null,
    $tbody : null,
    constructor : function ($table) {
        this.$table = $table;
        this.$thead = this.$table.find('thead');
        this.$table = this.$table.find('tbody');
    },

    init : function () {
        this.getUsers();
    },

    getUsers : function () {
        this.ajax(this.urls.getUsers,this.parame,this.getSuccess,this.error);
    },

    getSuccess : function (data) {
        if(data.status == 200){
            this.parame.index++;
            this.fillDatas(data.data);
        }
    },

    fillDatas : function (data) {
        var $tbody = $('');
        for ( var i = 0; i < data.length; i++){
            $tbody.append(this.fillData(data[i]));
        }
        this.$tbody.append($tbody);
    },

    fillData : function (data) {
        var $tr = $('<tr></tr>');
        for (var key in data){
            var $td = $('<td>'+htmlEncodeByRegExp(data[key])+'</td>');
            $tr.append($td);
        }
        return $tr;
    },

    ajax : function (url,data,fnSuccess,fnError) {
        $.ajax({url:url,type:'POST',dataType:'json',data:data,success:fnSuccess,error:fnError});
    },

    error: function (XMLHttpRequest, textStatus, errorThrown) {
        console.log(XMLHttpRequest);
        console.log(textStatus);
        console.log(errorThrown);
    }
};

function userList($table){
    this.$table = $table;
    this.$thead = this.$table.find('thead');
    this.$tbody = this.$table.find('tbody');
    this.limit = 20;
    this.parame = {
        orderBy : 'id',
        order : 'asc',
        index : 1,
        limit : this.limit
    };
    this.urls = {
        getUsers : host + 'admin_getAllUsers'
    };
    this.init = function(){
        this.getUsers();
    };
    this.getUsers = function(){
        this.ajax(this.urls.getUsers,this.parame,this.getSuccess,this.error);
    };
    this.ajax = function(url,data,fnSuccess,fnError){
        $.ajax({url:url,type:'POST',dataType:'json',data:data,success:fnSuccess,error:fnError});
    };
    this.getSuccess = function (data) {
        if(data.status == 200){
            console.log(userList.parame.index++);
            // this.fillDatas(data.data);
        }
    };
    this.fillDatas = function (data) {
        var $tbody = $('');
        for ( var i = 0; i < data.length; i++){
            $tbody.append(this.fillData(data[i]));
        }
        this.$tbody.append($tbody);
    };
    this.fillData = function (data) {
        var $tr = $('<tr></tr>');
        for (var key in data){
            var $td = $('<td>'+htmlEncodeByRegExp(data[key])+'</td>');
            $tr.append($td);
        }
        return $tr;
    };
    this.error = function (XMLHttpRequest, textStatus, errorThrown) {
        console.log(XMLHttpRequest);
        console.log(textStatus);
        console.log(errorThrown);
    }
}


admin.user.users = [];

admin.user.$table = null;

admin.user.$thead = null;

admin.user.$tbody = null;

admin.user.limit = 20;

admin.user.maxIndex = null;

admin.user.total = null;

admin.user.parame = {
    orderBy : 'id',
    order : 'asc',
    index : 1,
    limit : admin.user.limit
};

admin.user.init = function () {
    admin.user.$table = $('#user-table');
    admin.user.$thead = admin.user.$table.find('thead');
    admin.user.$tbody = admin.user.$table.find('tbody');
    admin.user.$table_info = $('#user-info');
    admin.user.getUsers(admin.user.getSuccess);
};

admin.user.getUsers = function(fnSuccess){
    admin.user.ajax(
        admin.urls.getUsers,
        admin.user.parame,
        fnSuccess,
        admin.user.error
    );
};

admin.user.ajax = function(url,data,fnSuccess,fnError){
    $.ajax({
        url:url,
        type:'POST',
        dataType:'json',
        data:data,
        xhrFields: {withCredentials: true},
        success:fnSuccess,
        error:fnError
    });
};

admin.user.getSuccess = function(data){
    if(data.status == 200){
        //admin.user.parame.index++;
        admin.user.total = data.data['total'];
        admin.user.maxIndex = Math.ceil(admin.user.total / admin.user.parame.limit);
        admin.user.pageChange();
        admin.user.fillHead(data.data['users'][0]);
        admin.user.fillDatas(data.data['users']);

        admin.user.page();
    }
    else{
        $.Huimodalalert(data.message,5000);
    }
};

admin.user.fillHead = function(data){
    var field = admin.user.field;
    var $tr = $('<tr></tr>');
    for (var key in data){
        var $td = $('<th class="'+key+'">'+field[key]+'</th>');
        $tr.append($td);
    }
    $tr.append('<th class="action">操作</th>');
    admin.user.$thead.append($tr);
};

admin.user.fillData = function (data) {
    var $tr = $('<tr></tr>');
    for (var key in data){
        var $td = $('<td class="'+key+'" title="'+data[key]+'">'+data[key]+'</td>');
        if(key == 'user_head_img'){
            var src = data[key];
            src = src == null ? 'http://static.h-ui.net/h-ui/images/ucnter/avatar-default.jpg' : src;
            $td.html('<img class="avatar size-L radius" src="'+src+'" alt="'+key+'" title="'+src+'">');
        }
        else if(key == 'user_city'){
            var info = data[key];
            for(var i = 0; i < City.length; i++){
                if(info == City[i]['CityID']){
                    info = City[i]['name'];
                    break;
                }
            }
            $td = $('<td class="'+key+'" title="'+info+'">'+info+'</td>');
        }
        else if(key == 'user_province'){
            var info = data[key];
            for(var i = 0; i < Province.length; i++){
                if(info == Province[i]['ProID']){
                    info = Province[i]['name'];
                    break;
                }
            }
            $td = $('<td class="'+key+'" title="'+info+'">'+info+'</td>');
        }
        $tr.append($td);
    }
    var $action = $('<td class="action"><span class="icon Hui-iconfont show">&#xe665;</span><span class="icon Hui-iconfont edit">&#xe60c;</span><span class="icon Hui-iconfont delete">&#xe6e2;</span></td>');
    $tr.append($action);
    $tr.find('.show').click(function(){
        admin.user.showUserInfo(data);
        modalshow();
    });
    return $tr;
};

admin.user.fillDatas = function(data) {
    admin.user.$table.find('tbody').remove();
    var $tbody = $('<tbody></tbody>');
    for ( var i = 0; i < data.length; i++){
        $tbody.append(admin.user.fillData(data[i]));
    }
    //console.log(admin.user.users);
    admin.user.$table.append($tbody);
};

admin.user.fillAuthorTbody = function (data) {
    var field = admin.user.field;
    var $tbody = $('<tbody></tbody>');
    for(var key in data){
        var $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l" title="'+data[key]+'">'+data[key]+'</td></tr>');
        if(key == 'user_head_img'){
            var src = data[key];
            src = src == null ? 'http://static.h-ui.net/h-ui/images/ucnter/avatar-default.jpg' : src;
            $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l"><img src="'+src+'" class="avatar size-L radius" alt="'+field[key]+'" title="'+data[key]+'"></td></tr>');
        }
        else if( key == 'user_card_img' || key == 'user_bg_img'){
            var src = data[key];
            src = src == null ? 'http://static.h-ui.net/h-ui/images/ucnter/avatar-default.jpg' : src;
            $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l"><img src="'+src+'" class="" alt="'+field[key]+'" title="'+data[key]+'"></td></tr>');
        }
        else if( key == 'user_city' ){
            var info = data[key];
            for(var i = 0; i < City.length; i++){
                if(info == City[i]['CityID']){
                    info = City[i]['name'];
                    break;
                }
            }
            $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l" title="'+info+'">'+info+'</td></tr>');
        }
        else if( key == 'user_province'){
            var info = data[key];
            for(var i = 0; i < Province.length; i++){
                if(info == Province[i]['ProID']){
                    info = Province[i]['name'];
                    break;
                }
            }
            $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l" title="'+info+'">'+info+'</td></tr>');
        }
        // else if ( key == 'user_bg_img'){
        //     var src = data[key];
        //     src = src == null ? 'http://static.h-ui.net/h-ui/images/ucnter/avatar-default.jpg' : src;
        //     $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l"><img src="'+src+'" class="" alt="'+field[key]+'" title="'+data[key]+'"></td></tr>');
        // }
        $tbody.append($tr);
    }
    return $tbody;
};

admin.user.showUserInfo = function(data){
    admin.user.$table_info.find('tbody').remove();
    //var field = admin.user.field;
    // var $tbody = $('<tbody></tbody>');
    // for(var key in data){
    //     var $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l" title="'+data[key]+'">'+data[key]+'</td></tr>');
    //     if(key == 'user_head_img'){
    //         var src = data[key];
    //         src = src == null ? 'http://static.h-ui.net/h-ui/images/ucnter/avatar-default.jpg' : src;
    //         $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l"><img src="'+src+'" class="avatar size-L radius" alt="'+field[key]+'" title="'+data[key]+'"></td></tr>');
    //     }
    //     else if( key == 'user_card_img' || key == 'user_bg_img'){
    //         var src = data[key];
    //         src = src == null ? 'http://static.h-ui.net/h-ui/images/ucnter/avatar-default.jpg' : src;
    //         $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l"><img src="'+src+'" class="" alt="'+field[key]+'" title="'+data[key]+'"></td></tr>');
    //     }
    //     else if( key == 'user_city' ){
    //         var info = data[key];
    //         for(var i = 0; i < City.length; i++){
    //             if(info == City[i]['CityID']){
    //                 info = City[i]['name'];
    //                 break;
    //             }
    //         }
    //         $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l" title="'+info+'">'+info+'</td></tr>');
    //     }
    //     else if( key == 'user_province'){
    //         var info = data[key];
    //         for(var i = 0; i < Province.length; i++){
    //             if(info == Province[i]['ProID']){
    //                 info = Province[i]['name'];
    //                 break;
    //             }
    //         }
    //         $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l" title="'+info+'">'+info+'</td></tr>');
    //     }
    //     // else if ( key == 'user_bg_img'){
    //     //     var src = data[key];
    //     //     src = src == null ? 'http://static.h-ui.net/h-ui/images/ucnter/avatar-default.jpg' : src;
    //     //     $tr = $('<tr><td class="text-r">'+field[key]+'</td><td class="text-l"><img src="'+src+'" class="" alt="'+field[key]+'" title="'+data[key]+'"></td></tr>');
    //     // }
    //     $tbody.append($tr);
    // }
    admin.user.$table_info.append(admin.user.fillAuthorTbody(data));
};

admin.user.error = function(XMLHttpRequest, textStatus, errorThrown){
    console.log(XMLHttpRequest);
    console.log(textStatus);
    console.log(errorThrown);
};

admin.user.nextSuccess = function (data) {
    if(data.status == 200){
        admin.user.pageChange();
        admin.user.fillDatas(data.data['users']);
        if(admin.user.parame.index >= admin.user.maxIndex){
            $('button.next').addClass('disabled');
        }
        else{
            $('button.next').removeClass('disabled');
        }
        $('button.prev').removeClass('disabled');
    }
    else{
        $.Huimodalalert(data.message,5000);
    }
};

admin.user.prevSuccess = function (data) {
    if(data.status == 200){
        admin.user.pageChange();
        admin.user.fillDatas(data.data['users']);
        if(admin.user.parame.index <= 1){
            $('button.prev').addClass('disabled');
        }
        else{
            $('button.prev').removeClass('disabled');
        }
        $('button.next').removeClass('disabled');
    }
    else{
        $.Huimodalalert(data.message,5000);
    }
};

admin.user.page = function () {
    var $prev = $('button.prev');
    var $next = $('button.next');

    $next.click(function () {
        $(this).addClass('disabled');
        if(admin.user.parame.index < admin.user.maxIndex){
            admin.user.parame.index++;
        }
        admin.user.getUsers(admin.user.nextSuccess);
    });

    $prev.click(function () {
        $(this).addClass('disabled');
        if(admin.user.parame.index > 1){
            admin.user.parame.index--;
        }
        admin.user.getUsers(admin.user.prevSuccess);
    });
};

admin.user.pageChange = function () {
    var $index = $('span.index');
    var $total = $('span.total');
    $index.html(admin.user.parame.index);
    $total.html(admin.user.maxIndex);
    if(admin.user.parame.index <= 1){
        $('button.prev').addClass('disabled');
    }

    if(admin.user.parame.index >= admin.user.maxIndex){
        $('button.next').addClass('disabled');
    }
};

// admin.user.limit = 20;
//
// admin.user.parame = {
//     orderBy : 'id',
//     order : 'asc',
//     index : 1,
//     limit : admin.user.limit
// };

// admin.user.urls = {
//     getUsers : host + 'admin_getAllUsers'
// };
//
// admin.user.getUsers = function () 